<?php
include_once "../header1.php";
?>
<script type="text/javascript">
function getHttpRequest()
{
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function showHint(str)
{
var xmlhttp;    
if (str=="")
  {
  document.getElementById("List").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   document.getElementById("List").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","upp_sch_names.php?q="+str,true);
xmlhttp.send();
}
function fill(val)
{
document.form1.SchName.value=val;
document.getElementById("List").innerHTML="";
var xmlhttp;    
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   document.getElementById("ExaNam").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","exaname.php?q="+val,true);
xmlhttp.send();
}
</script>
<script type="text/javascript">
function getExaSubject(exa)
{
var xmlhttp;  
var sch=document.forms["form1"]["SchName"].value;  
var hin=exa+","+sch; 
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   document.getElementById("Subject").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_Exa_Subject.php?q="+hin,true);
xmlhttp.send();
}
</script>
<script type="text/javascript">
function getExaDate(subj)
{
var xmlhttp;  
var sch=document.forms["form1"]["SchName"].value;
var exa=document.forms["form1"]["Exam_Name"].value;
var hin=subj+","+exa+","+sch;

if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   document.getElementById("Exa_year").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_Exa_date.php?q="+hin,true);
xmlhttp.send();
}
</script>  
</head>
<body>
<?php
include_once ("../Login/databaseconnection.php");

if(!loggedin())
{
	require_once ("../index.php");
}
    else
   {
   if(isset($_POST['SaveChanges']))
   {
   $Exam_Name=$_SESSION['Exam_Name'];
   $School_Id=$_SESSION['School_Id'];
	$Year=$_SESSION['Year'];
   	    $No_Of_Sat_Students=$_POST['No_Of_Sat_Students'];
    $No_Of_Passed_Students=$_POST['No_Of_Passed_Students'];
  if($_SESSION['Access_Type']=="Teacher in Charge")
{
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
  
   if($School_Id==$Own_School_Id)
	{
  require_once 'UPP.php';
   }
   else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
   }
   }
else if($_SESSION['Access_Type']=="Administrator")
{
require_once 'UPP.php';
}   
   else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
   }
    $School_Id="";
	   $Exam_Name="";
	   $Year="";
       if(isset($_POST['Edit_This']))
	   {
	  $SchName=$_POST['SchName'];
$name=explode(",",$SchName);
$nam=$name[0];
$tn=count($name);
for($i=1;$i<$tn;$i++)
{
if(empty($adr))
{
$adr=$name[$i];
}
else
{
$adr=$adr.",".$name[$i];
}
}
$results= mysql_query("SELECT * FROM school Where Name='$nam' AND Address='$adr'");
if($row = mysql_fetch_array($results))
{
 $School_Id=$row['School_Id'];
}
	   $Exam_Name=$_POST['Exam_Name'];
	   $Year=$_POST['Year'];
	   
	   $_SESSION['Exam_Name']=$Exam_Name;
   $_SESSION['School_Id']=$School_Id;
	  $_SESSION['Year']=$Year;
	  if($SchName==""||$Exam_Name=="Select One"||$Year=="Select One")
	   {
		    
	$_SESSION['errorin1']="Please select the Exam name,School's name and Year!";
}
	   else
	   {
	   if($_SESSION['Access_Type']=="Teacher in Charge")
{
$SchName=$_POST['SchName'];
$name=explode(",",$SchName);
$nam=$name[0];
$tn=count($name);
for($i=1;$i<$tn;$i++)
{
if(empty($adr))
{
$adr=$name[$i];
}
else
{
$adr=$adr.",".$name[$i];
}
}
$results= mysql_query("SELECT * FROM school Where Name='$nam' AND Address='$adr'");
if($row = mysql_fetch_array($results))
{
 $School_Id=$row['School_Id'];
}
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
  
   if($School_Id==$Own_School_Id)
	{
	$result=mysql_query("SELECT * FROM exam WHERE School_Id='$School_Id' AND Exam_Name='$Exam_Name' AND Year='$Year'");
	   if(mysql_num_rows($result)==0)
   {
$_SESSION['errornorow']="Very sorry there is no result,Please check your inputs and try again!";
   }
	}
	else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
	}
	 else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
	}
	   else if($_SESSION['Access_Type']=="Administrator")
{
	   $result=mysql_query("SELECT * FROM exam WHERE School_Id='$School_Id' AND Exam_Name='$Exam_Name' AND Year='$Year'");
	   if(mysql_num_rows($result)==0)
   {
$_SESSION['errornorow']="Very sorry there is no result,Please check your inputs and try again!";
   }
   }
   }
	 } 
   ?>
    <?php

include_once "../header2.php";


?>
<?php
include_once "topmenu.php";
?>
<div id="content">
	<div id="main">
	  <div id="bodyfield">
	   <div id="lefttablereg"><br/>
<form action="#" method="POST"name="form1">
  <table width="586" border="0" class="tablefontv">
    <tr>
      <td colspan="4"  scope="row" class="err"><?php
	if(isset($_POST['Edit_This'])||isset($_POST['SaveChanges']))
{
if(isset($_SESSION['errornotallo']))
{
	echo '<img src="../images/alert.jpg" width="12" height="12"/>';
echo $_SESSION['errornotallo'];
unset($_SESSION['errornotallo']);
}
 if(isset($_SESSION['error']))
	{
	echo '<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['error'];
		unset($_SESSION['error']);
	}
	if(isset($_SESSION['errorin']))
	{
	echo '<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['errorin'];
		unset($_SESSION['errorin']);
	}
	if(isset($_SESSION['errorin1']))
	{
	echo '<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['errorin1'];
		unset($_SESSION['errorin1']);
	}
	if(isset($_SESSION['errornorow']))
	{
	echo '<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['errornorow'];
		unset($_SESSION['errornorow']);
	}
}
		?>
		</td>
      </tr>
    <tr>
      <th width="30"  scope="row" bgcolor="#6AB5FF">&nbsp;</th>
      <th width="134"  scope="row" bgcolor="#6AB5FF">School's Name:</th>
      <td width="144" bgcolor="#999999">
              <?php
	 	if($_SESSION['Access_Type']=="Teacher in Charge")
{
	$NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id']; 
    $snam=mysql_query("SELECT * FROM school WHERE School_Id='$Own_School_Id'");
	  if($ros=mysql_fetch_array($snam))
	  {
	  $SchName=$ros['Name'].','.$ros['Address'];
	  }
}
	 echo "<input type='text' id='SchName' name='SchName' onKeyUp='showHint(this.value)' value='$SchName' size='40' readonly='readonly'/>";
}
	else if($_SESSION['Access_Type']=="Administrator")
{
	if(!isset($SchName))
	{
		$snam=mysql_query("SELECT * FROM school WHERE School_Id='$School_Idc'");
	  if($ros=mysql_fetch_array($snam))
	  {
	  $SchName=$ros['Name'].','.$ros['Address'];
	  }
	}
	    echo "<input type='text' id='SchName' name='SchName' onKeyUp='showHint(this.value)' value='$SchName' size='40'/>";
}
	  ?></td>
      <td width="260" bgcolor="#6AB5FF">&nbsp;</td>
    </tr>
    <tr>
      <th scope="row" bgcolor="#6AB5FF">&nbsp;</th>
      <th scope="row" bgcolor="#6AB5FF">Exam Name:</th>
      <td bgcolor="#999999">
	  <div id="ExaNam">
	  <select name="Exam_Name" onChange="getExaDate(this.value)">
        <?php
		if(isset($_POST['SaveChanges']))
	  {
	  $Ex_Name=$_SESSION['Exa_Name'];
	     echo '<option value="'.$Ex_Name.'">'.$Ex_Name.'</option>';
		 unset($_SESSION['Exa_Name']);
	  }
	  if(isset($_POST['Edit_This']))
	  {
	 echo '<option value="'.$Exam_Name.'">'.$Exam_Name.'</option>';
	  }
	  else
	  {
	  echo '<option value="Select One">Select One</option>';
	  }
	  ?>
        <?php
		if($School_Id!="")
		{
			$exa=mysql_query("SELECT DISTINCT(Exam_Name) FROM exam WHERE School_Id='$School_Id'");
		}
		if($School_Id=="")
		{
		 
	  $exa=mysql_query("SELECT DISTINCT(Exam_Name) FROM exam");
		}
	  
  while($row=mysql_fetch_array($exa))
  {
	 $Exa_Name=$row['Exam_Name'];
if($Exam_Name!=$Exa_Name)
{
	  echo '<option value="'.$Exa_Name.'">'.$Exa_Name.'</option>';
  }
  }
	  ?>
      </select></div></td>
      <td bgcolor="#6AB5FF">&nbsp;</td>
    </tr>
     <tr>
      <th height="23" scope="row" bgcolor="#6AB5FF"></th>
      <th height="23" scope="row" bgcolor="#6AB5FF">Subject:</th>
      <td bgcolor="#999999">  <div id="Subject"><select name="Subject" onchange="getExaDate(this.value)">
        <?php
	if(isset($_POST['Delete']))
	  {
	 echo '<option value="'.$Subject.'">'.$Subject.'</option>';
	  }
	  else
	  {
	  echo '<option value="Select One">Select One</option>';
	  }
	  ?>
        <?php
	 if($School_Id!="")
		{
			$exa=mysql_query("SELECT DISTINCT(Subject) FROM exam WHERE School_Id='$School_Id'");
		}
		if($School_Id=="")
		{		 
	  $exa=mysql_query("SELECT DISTINCT(Subject) FROM exam");
		}
  while($row=mysql_fetch_array($exa))
  {
	 $Subj=$row['Subject'];

	  echo '<option value="'.$Subj.'">'.$Subj.'</option>';
  }
  
	  ?>
      </select></div></td>
      <td bgcolor="#6AB5FF">&nbsp;</td>
    </tr>
    <tr>
      <th height="31" bgcolor="#6AB5FF" scope="row">&nbsp;</th>
      <th scope="row" bgcolor="#6AB5FF">Year:</th>
      <td bgcolor="#999999">
      <div id="Exa_year"><select name="Year">
        <?php
		if(isset($_POST['SaveChanges']))
	  {
	  	  $Ye=$_SESSION['Yea'];
		  echo '<option value="'.$Ye.'">'.$Ye.'</option>';
		   unset($_SESSION['Yea']);
	  }
	  if(isset($_POST['Edit_This']))
	  {
	 echo '<option value="'.$Year.'">'.$Year.'</option>';
	  }
	  else
	  {
	  echo '<option value="Select One">Select One</option>';
	  }
	  ?>
        <?php
	 $exa=mysql_query("SELECT DISTINCT(Year) FROM exam");
	  
  while($row=mysql_fetch_array($exa))
  {
  $Yea=$row['Year'];
if($Year!=$Yea)
{
	  echo '<option value="'.$Yea.'">'.$Yea.'</option>';
  }

	 } ?>
      </select>
      </div></td>
      <td bgcolor="#6AB5FF"></td>
	  <form action="#" method="post" name="form">
      <?php
      echo "<input type='hidden' name='sid' id='sid' value='$School_Id' size='5'/>";
      ?></form>
    </tr>
    <tr>
      <th height="31" bgcolor="#6AB5FF" scope="row">&nbsp;</th>
      <th colspan="2" bgcolor="#6AB5FF" scope="row"><br/>
        <a href="UpdateExam.php" class="reset">Reset</a>|
        <input name="Edit_This" align="middle" type="submit" value="Edit this" class="buttonok"/></th>
      <td class="err" bgcolor="#6AB5FF">&nbsp;</td>
    </tr>
  </table>
  <br/>
  
</form>
<form action="#" method="POST">
    <?php 
	if(isset($_POST['Edit_This']))
	   {
	   if($SchName!=""&&$Exam_Name!="Select One"&&$Year!="Select One")
	   {
	    if($_SESSION['Access_Type']=="Teacher in Charge")
{
$SchName=$_POST['SchName'];
$name=explode(",",$SchName);
$nam=$name[0];
$tn=count($name);
for($i=1;$i<$tn;$i++)
{
if(empty($adr))
{
$adr=$name[$i];
}
else
{
$adr=$adr.",".$name[$i];
}
}
$results= mysql_query("SELECT * FROM school Where Name='$nam' AND Address='$adr'");
if($row = mysql_fetch_array($results))
{
 $School_Id=$row['School_Id'];
}
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
  
   if($School_Id==$Own_School_Id)
	{
	$result=mysql_query("SELECT * FROM exam WHERE School_Id='$School_Id' AND Exam_Name='$Exam_Name' AND Year='$Year'");
	if(mysql_num_rows($result)>0)
   {
  echo '<table width="354" border="0" class="tablefontv">';
 while($row=mysql_fetch_array($result))
  {
    
$s_name=$row['School_Id'];
$Exam=$row['Exam_Name'];
$Ye=$row['Year'];
$No_Of_Sat=$row['No_Of_Sat_Students'];
$No_Of_Passed=$row['No_Of_Passed_Students'];
	
   ?>
   <?php
      echo '<tr>';
        echo '<td width="200" height="44" scope="row">';
		echo "School Id:";
		echo '</td>';
        echo '<td width="144">';
		echo '<div contenteditable>';
		echo "<input type='text' name='School_Idc' value='$s_name'/>";
		echo '</div>';
		echo '</td>';
      echo '</tr>';
	  
      echo '<tr>';
        echo '<td height="44" scope="row">';
		echo "Exam Name:";
		echo '</td>';
        echo '<td>';
		echo '<div contenteditable>';
		echo "<input type='text' name='Exam_Namec' value='$Exam'/>";
		echo '</div>';
		echo '</td>';
      echo '</tr>';
	  
      echo '<tr>';
        echo '<td height="44" scope="row">';
		echo "Year:";
		echo '</td>';
        echo '<td>';
		echo '<div contenteditable>';
		echo "<input type='text' name='Yearc' value='$Ye'/>";
		echo '</div>';
		echo '</td>';
		 echo '</tr>';
		 
      echo '<tr>';
        echo '<td height="44" scope="row">';
		echo "Number Of Sat Students:";
		echo '</td>';
       echo '<td>';
	   echo '<div contenteditable>';
	   echo "<input type='text' name='No_Of_Sat_Students' value='$No_Of_Sat'/>";
	   echo '</div>';
	   echo '</td>';
      echo '</tr>';
	  
     echo '<tr>';
       echo '<td height="44" scope="row">';
	   echo "Number Of Passed Students:"; 
	    echo '</td>';
	  echo '<td>';
	   echo '<div contenteditable>';
	   echo "<input type='text' name='No_Of_Passed_Students' value='$No_Of_Passed'/>";
	   echo '</div>';
	   echo '</td>';
	   echo '</tr>';
	 }
   echo '</table>';
   
   echo "</br>";
   echo '<input name="SaveChanges" align="middle" type="submit" value="Save changes" class="buttonok"/>';
   }
	}
	}
	}
	else if($_SESSION['Access_Type']=="Administrator")
{
   $result=mysql_query("SELECT * FROM exam WHERE School_Id='$School_Id' AND Exam_Name='$Exam_Name' AND Year='$Year'");
   if(mysql_num_rows($result)>0)
   {
  echo '<table width="354" border="0" class="tablefontv">';
 while($row=mysql_fetch_array($result))
  {    
$s_name=$row['School_Id'];
$Exam=$row['Exam_Name'];
$Ye=$row['Year'];
$No_Of_Sat=$row['No_Of_Sat_Students'];
$No_Of_Passed=$row['No_Of_Passed_Students'];
	
   ?>
   <?php
      echo '<tr>';
        echo '<td width="200" height="44" scope="row">';
		echo "School Id:";
		echo '</td>';
        echo '<td width="144">';
		echo '<div contenteditable>';
		echo "<input type='text' name='School_Idc' value='$s_name'/>";
		echo '</div>';
		echo '</td>';
      echo '</tr>';
	  
      echo '<tr>';
        echo '<td height="44" scope="row">';
		echo "Exam Name:";
		echo '</td>';
        echo '<td>';
		echo '<div contenteditable>';
		echo "<input type='text' name='Exam_Namec' value='$Exam'/>";
		echo '</div>';
		echo '</td>';
      echo '</tr>';
	  
      echo '<tr>';
        echo '<td height="44" scope="row">';
		echo "Year:";
		echo '</td>';
        echo '<td>';
		echo '<div contenteditable>';
		echo "<input type='text' name='Yearc' value='$Ye'/>";
		echo '</div>';
		echo '</td>';
		 echo '</tr>';
		 
      echo '<tr>';
        echo '<td height="44" scope="row">';
		echo "Number Of Sat Students:";
		echo '</td>';
       echo '<td>';
	   echo '<div contenteditable>';
	   echo "<input type='text' name='No_Of_Sat_Students' value='$No_Of_Sat'/>";
	   echo '</div>';
	   echo '</td>';
      echo '</tr>';
	  
     echo '<tr>';
       echo '<td height="44" scope="row">';
	   echo "Number Of Passed Students:"; 
	    echo '</td>';
	  echo '<td>';
	   echo '<div contenteditable>';
	   echo "<input type='text' name='No_Of_Passed_Students' value='$No_Of_Passed'/>";
	   echo '</div>';
	   echo '</td>';
	   echo '</tr>';
	 }
   echo '</table>';
   
   echo "</br>";
   echo '<input name="SaveChanges" align="middle" type="submit" value="Save changes" class="buttonok"/>';
 }
 }
 }
	   }
	   ?>
       </form>
    </div>
 <div id="righttablereg">
  <div class="suggestionListsch" id="List"></div>
 </div>
</div>
</div>
</div>
<div id="sidebar"><br/>
<div id="partners" class="boxed">
			<h2 class="title">EXAMS</h2>
            </div>
<?php
include_once "menu.php";
?>
<?php
include_once "../footer.php";
?>
<?php

}
?>
</body>
</html>